package gui.edit.utils;



import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeMap;

import tuio.components.CheckBoxTouch;

import controls.DataControl;
import controls.edit.ParamsControl;

/**
 * @author Charles Perin
 * 29 mars 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 *
 */
public class ActorsMap extends TreeMap<String,Actor>{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7488918752084696264L;

	private ParamsControl paramsControl;
	
	public ActorsMap(DataControl dataControl){
		super();
		setParamsControl(dataControl.getParamsControl());
	}

	public ArrayList<CheckBoxTouch> getSelectedActorsCheckBoxes(){
		ArrayList<CheckBoxTouch> list = new ArrayList<CheckBoxTouch>();
		Iterator<String> iter = this.keySet().iterator();
		while(iter.hasNext()){
			String name = iter.next();
			Actor a = get(name);
			if(a.isSelected())list.add(a.getCheckBox());
		}
		return list;
	}

	public ArrayList<Actor> getSelectedAndNotIsolatedActors(){
		ArrayList<Actor> list = new ArrayList<Actor>();
		Iterator<String> iter = this.keySet().iterator();
		while(iter.hasNext()){
			String name = iter.next();
			Actor a = this.get(name);
			if(a.isSelected() && !a.isIsolated())list.add(a);
		}
		return list;
	}

	public ArrayList<String> getAllActorsNames(){
		ArrayList<String> names = new ArrayList<String>();
		Iterator<String> iter = this.keySet().iterator();
		while(iter.hasNext()){
			String name = iter.next();
			names.add(name);
		}
		return names;
	}

	public void setSelectedActors(Object[] selections) {
		Iterator<Actor> iter = this.values().iterator();
		while(iter.hasNext()){
			Actor a = iter.next();
			a.setSelected(false);
		}
		for(Object s:selections){
			this.get(s).setSelected(true);
		}
	}

	public ArrayList<ActorsPair> getActorRelations(){
		ArrayList<ActorsPair>list = new ArrayList<ActorsPair>();
		ArrayList<Actor> selectedAndNotIsolatedActors = getSelectedAndNotIsolatedActors();
		if(selectedAndNotIsolatedActors.size()>1){
			Iterator<Actor> iter = selectedAndNotIsolatedActors.iterator();
			while(iter.hasNext()){
				Actor a = iter.next();
				for(int i=1;i<selectedAndNotIsolatedActors.size();i++){
					list.add(new ActorsPair(a, selectedAndNotIsolatedActors.get(i)));
				}
				iter.remove();
			}
		}
		return list;
	}

	public void setParamsControl(ParamsControl paramsControl) {
		this.paramsControl = paramsControl;
	}

	public ParamsControl getParamsControl() {
		return paramsControl;
	}


}
